package localizer.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

public class RingSilentMode extends Activity{
	public static long TIME_TO_WAIT = 1000 * 8;
	MediaPlayer mp ;
	Config config;
	Thread th;
	boolean continua;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ring_silent_mode);
		continua =true;
		
		config = Config.getInstance(getApplicationContext());
		TIME_TO_WAIT = Long.parseLong(config.getTimeRing(getApplicationContext())) * 1000;
		Button btn = (Button) findViewById(R.id.ok_silent_mode);
		
		final Handler handler = new Handler(){
        	@Override
        	public void handleMessage(Message msg) {
        		fim();
        	}

        };
      
        final Handler handler2 = new Handler(){
        	@Override
        	public void handleMessage(Message msg) {
        		float  brilho = (Float) msg.obj;
        		Window window = getWindow(); 
				 LayoutParams layoutParams = window.getAttributes(); 
				 layoutParams.screenBrightness = brilho; 
				 layoutParams.flags=layoutParams.FLAG_KEEP_SCREEN_ON;
				 window.setAttributes(layoutParams);
//				 android.provider.Settings.System.putInt(RingSilentMode.this.getBaseContext().getContentResolver(), 
//							android.provider.Settings.System.SCREEN_BRIGHTNESS,brilho);
        	}

        };
		try{
		th = new Thread(){
			public void run(){
				try{
					long inicio = System.currentTimeMillis();
					float brilho = 0.02f;
					while(continua && System.currentTimeMillis() - inicio < TIME_TO_WAIT){
						
						Message m = handler2.obtainMessage();
						m.obj=new Float(brilho);
						handler2.sendMessage(m);
						if(brilho < 1){
							brilho=1;
							sleep(200);
						}else{
							brilho =0.02f;
							sleep(2000);
						}
						Log.e("Brilho"," "+brilho);
						
					}
					Message m = handler.obtainMessage();
					handler.sendMessage(m);
					} catch(Exception e){
						Log.e("erro na criacao da thread", e.getMessage());
				}
			}
		};
		th.start();
		} catch ( Exception e){
			Log.e("erro na criacao da thread", e.getMessage());
		}
		
		
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try{
					continua=false;
					fim();
				} catch(Exception e){
					
				}
			}
		});
	}

	public void fim(){
		Log.d("silencioso", "encerrou");
		try {
			mp.stop();			
			mp.release();
			mp = null;
		} catch (Exception e) {
		}
		finish();
	}
}
